package com.F04.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.F04.actionForm.F0404ActionForm;
import com.dao.common.DbOperationExt;
import com.util.CommonTools;

public class F0404Dao extends DbOperationExt {

	Logger log = Logger.getLogger(F0404Dao.class);

	public F0404Dao() {

	}

	public F0404Dao(Connection conn) {
		super(conn);
	}

	public List select(F0404ActionForm actionForm) throws SQLException {

		List list = new ArrayList();
		Map map = null;
		StringBuffer sql = new StringBuffer();

		String name = CommonTools.null2String(actionForm.getSupplierName());
		String family = CommonTools.null2String(actionForm.getSupplierFamily());

		sql.append("select code, supplier,address ");
		sql.append("	from pc_suppliers ");
		
//		查询条件
		if (!"".equals(name)) {
			sql.append(" where supplier like '%" + name + "%'");
		}

		ResultSet rest = null;
//		int i = 1;
		rest = super.executeQuery(sql.toString());
		while (rest.next()) {
			map = new HashMap();
//			map.put("id", i);
			map.put("supplierCode", CommonTools.null2String(rest
					.getString("code")));
			map.put("supplierName", CommonTools.null2String(rest
					.getString("supplier")));
			map.put("supplierAddr", CommonTools.null2String(rest
					.getString("address")));
			list.add(map);
//			i++;
		}
		// 未检索到数据
		if (map == null) {
			actionForm.setErrorFlag("2");
		}
		return list;
	}

	public void selectByCode(F0404ActionForm actionForm) throws SQLException {

		StringBuffer sql = new StringBuffer();

		String code = actionForm.getSupplierCode();
		
		sql.append("select supplier,address,telephone_number,fax_number,contact_name,vat ");
		sql.append("	from pc_suppliers ");
		sql.append(" where code = '"+code+"'");
		log.info(sql.toString());

		ResultSet rest = null;
		rest = super.executeQuery(sql.toString());
		while (rest.next()) {
			actionForm.setContractName(CommonTools.null2String(rest
					.getString("contact_name")));
			actionForm.setSupplierAddr(CommonTools.null2String(rest
					.getString("address")));
			actionForm.setSupplierName(CommonTools.null2String(rest
					.getString("supplier")));
			actionForm.setVAT(rest.getInt("vat")+"");
			actionForm.setPhoneNumber(CommonTools.null2String(rest
					.getString("telephone_number")));
			actionForm.setFaxNumber(CommonTools.null2String(rest
					.getString("fax_number")));
		}
	}
	
	public void update(F0404ActionForm actionForm) throws SQLException {

		StringBuffer sql = new StringBuffer();
		
		sql.append("update pc_suppliers set supplier = '"+actionForm.getSupplierName()+"',");
		sql.append("address='"+actionForm.getSupplierAddr()+"',");
		sql.append("telephone_number='"+actionForm.getPhoneNumber()+"',");
		sql.append("fax_number='"+CommonTools.null2String(actionForm.getFaxNumber())+"',");
		sql.append("contact_name='"+actionForm.getContractName()+"',");
		sql.append("vat="+actionForm.getVAT());
		sql.append(" where code = '"+actionForm.getSupplierCode()+"'");
		
		super.executeUpdate(sql.toString());
		
	}
	
	public void insert(F0404ActionForm actionForm) throws SQLException {

		String name = actionForm.getSupplierName();
		String addr = actionForm.getSupplierAddr();
		String VAT = actionForm.getVAT();
		String contactName = actionForm.getContractName();
		String fax = CommonTools.null2String(actionForm.getFaxNumber());
		String family = actionForm.getSupplierFamily();
		String tel = actionForm.getPhoneNumber();
		
		StringBuffer sql = new StringBuffer();
		sql.append("insert into pc_suppliers(supplier,address,vat,telephone_number,fax_number,contact_name)");
		sql.append(" values ('" + name + "','" + addr + "'," + VAT + ",'"
				+ tel + "','" + fax + "','" + contactName
				+ "')");
		super.executeUpdate(sql.toString());
	}
	
	public void deleteByCode(F0404ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		sql.append("delete from pc_suppliers where code = '"+CommonTools.null2String(actionForm.getId())+"'");
		super.executeUpdate(sql.toString());
	}
}
